package kr.bae.androidcomplete01.chapter08;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MoveCircle extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final MyView myview = new MyView(this);
		myview.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				if(myview.paint.getColor() == Color.BLUE) {
					myview.paint.setColor(Color.YELLOW);
					Toast.makeText(MoveCircle.this, "YELLOW", Toast.LENGTH_SHORT).show();
					myview.invalidate();
				}
				else {
					myview.paint.setColor(Color.BLUE);
					Toast.makeText(MoveCircle.this, "BLUE", Toast.LENGTH_SHORT).show();
					myview.invalidate();
				}
				return false;
			}
		}); 
		
		setContentView(myview);
	}
	
	class MyView extends View {
		float mx;
		float my;
		int mColor;
		Paint paint;
		
		public MyView(Context context) {
			super(context);
			mx = 100;
			my = 100;
			mColor = Color.BLUE;
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(mColor);
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);
			if(event.getAction() == MotionEvent.ACTION_DOWN) {
				mx = event.getX();
				my = event.getY();
				invalidate();
				return true;
			}
			return false;
		}
		
		
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.LTGRAY);
			canvas.drawCircle(mx, my, 16, paint);
		}
		
	}
}
